package com.chixing.dao.impl;

import com.chixing.dao.RowMapper;
import com.chixing.util.DBConnection;
import com.mysql.cj.protocol.a.LocalDateValueEncoder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JDBCTemplate<E> {

    private RowMapper<E> rowMapper;
    public JDBCTemplate(){}
    public JDBCTemplate(RowMapper<E> rowMapper) {
        this.rowMapper = rowMapper;
    }

    public void setRowMapper(RowMapper<E> rowMapper) {
        this.rowMapper = rowMapper;
    }

    /**
     * 增删改操作
     * @param sql   要执行的sql语句
     * @param args  实参，就是占位符里面的参数
     * @return  影响行数
     */
    public int update(String sql,Object... args){
        Connection connection = DBConnection.getConnection();//连接数据库
        PreparedStatement pstmt =null;
        int rows =0;
        try {
            pstmt = connection.prepareStatement(sql);//加载sql语句
            //给占位符传参
            for (int i = 0; i < args.length; i++) {
                pstmt.setObject(i+1,args[i]);
            }
            //获取影响行数
            rows = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            if (pstmt!=null)
                DBConnection.closeConnection();
        }
        return rows;
    }

    /**
     * 按住键单个查询
     * @param sql   要执行的sql语句
     * @param args  实际参数
     * @return  查询的对象
     */
    public E selectForObject(String sql,Object... args){
        E e = null;
        Connection connection = DBConnection.getConnection();//连接数据库
        PreparedStatement pstmt =null;
        ResultSet rs =null;
        try {
            //加载sql语句
            pstmt = connection.prepareStatement(sql);
            //设置占位符的值
            for (int i = 0; i < args.length; i++) {
                pstmt.setObject(i+1,args[i]);
            }
            rs = pstmt.executeQuery();
            while (rs.next()){
                e = rowMapper.mapper(rs);
            }
        } catch (SQLException exception) {
            throw new RuntimeException(exception);
        }finally {
            if (rs!=null) {
                try {
                    rs.close();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            if (pstmt!=null) {
                try {
                    pstmt.close();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            if (connection!= null)
                DBConnection.closeConnection();
        }
        return e;
    }

    /**
     * 返回多个查找对象
     * @param sql   要执行的sql语句
     * @param args  实际参数
     * @return  返回的对象列表
     */
    public List<E> selectForList(String sql,Object... args){
        List<E> list=new ArrayList<>();

        Connection connection = DBConnection.getConnection();
        PreparedStatement pstmt =null;
        try {
            pstmt = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                pstmt.setObject(i+1,args[i]);
            }
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                E e  = rowMapper.mapper(rs);
                list.add(e);
            }
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        return list;
    }
}
